﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IS.Comm;
namespace senddemo
{
    public partial class frmSend : Form
    {
        string IP = "";
        int port = 2345;
        CommonLib com = new CommonLib();
        public frmSend()
        {
            InitializeComponent();
        }

        private void cmdCheck_Click(object sender, EventArgs e)
        {
            //port=com.convert2Int(txtPort.Text,2345);
            //if(!sendReceive.checkConnect(txtIP.Text,port))
            //{
            //    MessageBox.Show("Không kết nối được với server");
            //}
        }
        sendCommand scm = null;
        int countPa = 0;
        private void cmdSend_Click(object sender, EventArgs e)
        {
            lblConnect.Text = "";
            int v = com.convert2Int(txtData.Text, 1234);
            port = com.convert2Int(txtPort.Text, 2345);
            if (scm == null)
            {
                scm = new sendCommand(txtIP.Text, port);
                scm.connect();
            }
            if (!scm.isConnected())
            {
                scm.connect();
            }
            if (scm.isConnected())
            {
                countPa++;
                scm.sendDemo(v,ref countPa);
            }
            else
            {
                lblConnect.Text = "Cant connect";
            }
            
        }
        int c = 0;
        int cf = 0;
        private void tmSend_Tick(object sender, EventArgs e)
        {
            int v = com.convert2Int(txtData.Text, 1234);
            port = com.convert2Int(txtPort.Text, 2345);
            if (scm == null)
            {
                scm = new sendCommand(txtIP.Text, port);
                scm.connect();
            }
            if (!scm.isConnected())
            {
                scm.connect();
            }
            if (scm.isConnected())
            {
                c++;
                countPa++;
                if (scm.sendDemo(v, ref countPa,1234,0) > 0)
                {
                    lblCount.Text = c.ToString();
                }
                if (scm.sendDemo(v, ref countPa,2345,5) > 0)
                {
                    lblCount.Text = c.ToString();
                }
                if (scm.sendDemo(v, ref countPa,3456,10) > 0)
                {
                    lblCount.Text = c.ToString();
                }
                if (scm.sendDemo(v, ref countPa,4567,15) > 0)
                {
                    lblCount.Text = c.ToString();
                }
                if (scm.sendDemo(v, ref countPa,5678,20) > 0)
                {
                    lblCount.Text = c.ToString();
                }
                else
                {
                    cf++;
                    lblSendFail.Text = cf.ToString();

                }
            }
            else
            {
                lblConnect.Text = "Can't connect";
            }
        }

        private void cmdTimerSend_Click(object sender, EventArgs e)
        {
            tmSend.Enabled = !tmSend.Enabled;
            if (tmSend.Enabled)
            {
                c = 0;
                cf = 0;
                lblConnect.Text = "";
                cmdTimerSend.Text = "Stop timer";
            }
            else
            {
                cmdTimerSend.Text = "Start timer";
            }
        }

        private void cmdSendPre_Click(object sender, EventArgs e)
        {
            //33 54 43 55 0 0 23 29 40 1F 0 0 A 34 D F A D 14 3B 45 63 69 2F 47 13 0 80 E1 0 0 7 0 0 0 0 0 1 0 32 2D A F A D 0 D C 0 A C8 1 A1  2A D A

            if (scm == null)
            {
                scm = new sendCommand(txtIP.Text, port);
                scm.connect();
            }
            if (!scm.isConnected())
            {
                scm.connect();
            }
            if (scm.isConnected())
            {
                scm.sendBinary();
            }
            else
            {
                lblConnect.Text = "Can't connect";
            }

        }
    }
}
